package cn.iocoder.yudao.module.cms.keyword.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.cms.keyword.dal.dataobject.KeywordDO;
import cn.iocoder.yudao.module.cms.keyword.service.KeywordService;
import cn.iocoder.yudao.module.cms.keyword.vo.KeywordPageReqVO;
import cn.iocoder.yudao.module.cms.keyword.vo.KeywordRespVO;
import cn.iocoder.yudao.module.cms.keyword.vo.KeywordSaveReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 App - 搜索词")
@RestController
@RequestMapping("/cms/keyword")
@Validated
public class AppKeywordController {

    @Resource
    private KeywordService keywordService;

    @PostMapping("/save")
    @Operation(summary = "创建或更新搜索词")
    public CommonResult<Boolean> updateKeyword(@Valid @RequestBody KeywordSaveReqVO updateReqVO) {
        keywordService.saveKeyword(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得搜索词")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PermitAll
    public CommonResult<KeywordRespVO> getKeyword(@RequestParam("id") Long id) {
        KeywordDO keyword = keywordService.getKeyword(id);
        return success(BeanUtils.toBean(keyword, KeywordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得搜索词分页")
    @PermitAll
    public CommonResult<PageResult<KeywordRespVO>> getKeywordPage(@Valid KeywordPageReqVO pageReqVO) {
        PageResult<KeywordDO> pageResult = keywordService.getKeywordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeywordRespVO.class));
    }

}
